<template>
  <div class="dev">
    <!-- 搜索框 -->
    <div class="search">
      <!-- 日期时间选择器 -->
      <div class="block">
        <el-date-picker v-model="time" value-format="yyyy-MM-dd HH:mm:ss" type="datetimerange" align="center"
          start-placeholder="开始日期" end-placeholder="结束日期" range-separator="至" :default-time="['00:00:00', '23:59:59']">
        </el-date-picker>
      </div>
      <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
    </div>
    <!-- 表格数据 -->
    <div class="table">
      <el-table :data="tableData" ref="isExpand" v-loading="loading" border height="calc(100%-10rem)"
        highlight-current-row stripe style="width: 100%">
        >
        <template slot="empty">
          <el-empty description="暂无数据"></el-empty>
        </template>
        <el-table-column align="center" prop="createUser" label="导入人">
        </el-table-column>
        <el-table-column align="center" prop="importCount" label="导入数量">
        </el-table-column>
        <el-table-column align="center" prop="description" label="备注">
        </el-table-column>
        <el-table-column align="center" prop="createTime" label="导入时间">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination v-show="tableData.length !== 0" background @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page="pageNum" :page-sizes="[5, 10, 15, 20, 25, 30]"
        :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 编辑备注 -->
    <div class="edit">
      <el-dialog :close-on-click-modal="false" title="备注" :visible.sync="DialogVisible" width="600px">
        <el-form :model="editForm" ref="editForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="备注" prop="description">
            <el-input type="textarea" :rows="4" v-model="editForm.description" clearable placeholder="请输入备注"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="DialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editFn">确 认</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRecordApi, putRecordApi } from "../../../../api/system/import";
export default {
  data() {
    return {
      loading: false,
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total: null,
      DialogVisible: false,
      time: "",
      startTime: "",
      endTime: "",
      editForm: {
        description: "",
      },
    };
  },
  created() {
    this.pageSize = Number(sessionStorage.getItem("pageSize")) || 10;
    this.pageNum = Number(sessionStorage.getItem("pageNum")) || 1;
    this.getRecord(this.pageNum, this.pageSize);
  },
  destroyed() {
    sessionStorage.removeItem("pageNum");
    sessionStorage.removeItem("pageSize");
  },
  methods: {
    // 获取电表列表
    getRecord(pageNum, pageSize, startTime, endTime) {
      this.loading = true;
      getRecordApi(pageNum, pageSize, startTime, endTime).then((res) => {
        this.total = res.data.total;
        this.loading = false;
        this.tableData = res.data.rows;
      });
    },
    // 查询
    search() {
      if (this.time) {
        this.startTime = this.time[0];
        this.endTime = this.time[1];
      } else {
        (this.startTime = ""), (this.endTime = "");
      }
      this.getRecord(this.pageNum, this.pageSize, this.startTime, this.endTime);
    },
    // 分页
    handleSizeChange(val) {
      this.getRecord(this.pageNum, val, this.startTime, this.endTime);
      sessionStorage.setItem("pageSize", val);
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.getRecord(val, this.pageSize, this.startTime, this.endTime);
      sessionStorage.setItem("pageNum", val);
      this.pageNum = val;
    },
    edit(e) {
      this.DialogVisible = true;
      this.editForm.description = e.description;
      this.editId = e.importId;
    },
    editFn() {
      putRecordApi(this.editId, this.editForm)
        .then((res) => {
          this.$message.success("修改备注成功");
          this.DialogVisible = false;
          this.search()
        })
        .catch((err) => {
          this.$message.error("修改备注失败！");
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.dev {
  padding-top: 15px;
  padding: 0 15px;
  height: calc(100vh - 100px);

  .search {
    margin-bottom: 15px;
    height: 40px;

    /deep/ .block {
      // width: 200px;
      position: relative;
      float: left;
    }

    .el-input {
      float: left;
      width: 150px;
      margin-left: 15px;
    }

    .el-button {
      width: 100px;
      margin-left: 15px;
      float: left;
    }

    .btn {
      width: 150px;
    }
  }

  .table {
    height: 100%;

    .el-table {
      height: 70%;
    }

    .el-pagination {
      text-align: center;
      margin-top: 20px;
    }
  }
}
</style>